﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.WebApp
{
    public partial class UseGiftCard : System.Web.UI.Page
    {
        User user;
        User recipientUser = new User();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            user = UserDataAccess.GetUserByEmail(User.Identity.Name);
            this.ForSomElseLabel.Text = Request["ToSomeElse"];
            this.QuantityLabel.Text = Request["Quantity"];
            this.CurrencyLabel.Text = Request["Currency"];
            LoadMessageLabel(this.ForSomElseLabel.Text,user);
        }

        protected void LoadMessageLabel(string state,User user)
        {
            if (state == "true")
            {
                this.WelcomeLabel.Text = "You've decided to give a gift card to someone : ";
                QuantityAndCurrency(this.QuantityLabel.Text,this.CurrencyLabel.Text);
                DivElementsVisible();

            }
            else 
            {
                
                this.WelcomeLabel.Text = "It was added to your account : ";
                QuantityAndCurrency(this.QuantityLabel.Text, this.CurrencyLabel.Text);
                GiftCardManager.NewGiftCard(user.Id, user.Id, float.Parse(QuantityL.Text), CurrencyL.Text, "yes", "yes");
            }
        }
        protected void QuantityAndCurrency(string quantity, string currency)
        {
            this.QuantityL.Text = this.QuantityLabel.Text;
            this.QuantityL.Visible = true;
            this.CurrencyL.Text = this.CurrencyLabel.Text;
            this.CurrencyL.Visible = true;
        }

        protected void OkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?");
        }
        protected void DivElementsVisible()
        {
            this.TextBox1.Visible = true;
            this.FindButton.Visible = true;
            this.GridView1.Visible = true;

        }

        protected void FindButton_Click(object sender, EventArgs e)
        {
            GridView1.DataSource = FindWishListManager.GetUserBySpecificName(TextBox1.Text);
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            this.recipientUser.Id = Convert.ToInt64(row.Cells[1].Text);
            GiftCardManager.NewGiftCard(this.user.Id, this.recipientUser.Id, float.Parse(QuantityL.Text), CurrencyL.Text, "no", "yes");
            Response.Redirect("RecipientGiftCard.aspx?RecipientName="+ row.Cells[2].Text+ "&RecipientLastName="+ row.Cells[3].Text +"&Quantity="+QuantityLabel.Text+"&Currency="+ CurrencyLabel.Text);

        }

       
    }
}